package com.atguigu.gmall.all.controller;

import com.atguigu.gmall.list.client.ListFeignClient;
import com.atguigu.gmall.model.list.SearchParam;
import com.atguigu.gmall.model.list.SearchResponseVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestBody;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author: 李旭
 * @create: 2021-09-04 15:25
 *
 *   搜索
 *
 **/
@Controller
public class ListController {

    @Autowired
    private ListFeignClient listFeignClient;
    //进入搜索页面
    @GetMapping("/list.html")
    public String list(SearchParam searchParam, Model model){
        //1:入参对象
        model.addAttribute("searchParam",searchParam);
        SearchResponseVo vo = listFeignClient.search(searchParam);
        //2:品牌集合
        model.addAttribute("trademarkList",vo.getTrademarkList());
        //3:平台属性集合
        model.addAttribute("attrsList",vo.getAttrsList());
        //4:商品集合
        model.addAttribute("goodsList",vo.getGoodsList());
        //5:分页
        model.addAttribute("pageNo",vo.getPageNo());
        model.addAttribute("totalPages",vo.getTotalPages());
        //6:urlParam  路径接接
        String urlParam = buildUrlParam(searchParam);
        model.addAttribute("urlParam",urlParam);
        //7:  trademarkParam  回显已选中的品牌
        String trademarkParam = buildTrademarkParam(searchParam);
        model.addAttribute("trademarkParam",trademarkParam);
        //8: propsParamList  已选中平台属性集合
        List<Map>  propsParamList = buildPropsParamList(searchParam);
        model.addAttribute("propsParamList",propsParamList);
        //9:回显 排序  红底  升或降
        Map orderMap = buildOrderMap(searchParam);
        model.addAttribute("orderMap",orderMap);
        return "list/index";
    }

    //回显 排序  红底  升或降
    private Map buildOrderMap(SearchParam searchParam) {
        // order=1:asc
        String order = searchParam.getOrder();
        Map map = new HashMap();
        if(!StringUtils.isEmpty(order)){
            String[] o = order.split(":");
            map.put("type",o[0]);
            map.put("sort",o[1]);
        }else{
            //走默认  综合
            map.put("type","1");
            map.put("sort","desc");
        }
        return map;
    }

    //已选中平台属性集合
    private List<Map> buildPropsParamList(SearchParam searchParam) {
        //props=23:4G:运行内存      平台属性ID:平台属性值名称：平台属性名称
        String[] props = searchParam.getProps();
        if(null != props && props.length > 0){
            return  Arrays.stream(props).map(prop -> {
                String[] p = prop.split(":");
                Map map = new HashMap();
                map.put("attrId",p[0]);
                map.put("attrName",p[2]);
                map.put("attrValue",p[1]);
                return map;
            }).collect(Collectors.toList());
        }
        return null;
    }

    //回显已选中的品牌
    private String buildTrademarkParam(SearchParam searchParam) {
        //trademark=2:华为
        String trademark = searchParam.getTrademark();
        if(!StringUtils.isEmpty(trademark)){
            String[] t = trademark.split(":");
            return "品牌:" + t[1];
        }
        return null;
    }

    //路径拼接
    private String buildUrlParam(SearchParam searchParam) {
        StringBuilder sb = new StringBuilder();
        String keyword = searchParam.getKeyword();
        //if(!StringUtils.isEmpty(keyword)){
            sb.append("http://list.gmall.com/list.html?keyword=").append(keyword);
        //}
        String trademark = searchParam.getTrademark();
        if(!StringUtils.isEmpty(trademark)){
            sb.append("&trademark=").append(trademark);
        }
        //一堆平台属性
        String[] props = searchParam.getProps();
        if(null != props && props.length > 0){
            for (String prop : props) {
                //props=23:4G:运行内存
                sb.append("&props=").append(prop);
            }
        }
        return sb.toString();
    }

}
